﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using System.Web;
using System.Web.SessionState;
using System.IO;
using A2DFramework.SessionService.Configuration;

namespace A2DFramework.SessionService
{
    internal class Helper
    {
        public static bool ParseSessionID(string rawSessionId, out string prefix, out string realSessionId)
        {
            prefix = string.Empty;
            realSessionId = string.Empty;

            if (rawSessionId == null)
                return false;
            if (rawSessionId.Trim().Length == 0)
                return false;
            string[] parsedValues = rawSessionId.Split('.');
            if (parsedValues == null)
                return false;
            if (parsedValues.Length != 2)
                return false;

            prefix = parsedValues[0];
            realSessionId = parsedValues[1];

            return true;
        }

        public static MongoCollection<MongoDBSessionEntity> GetMongoDBCollection(string sessionId)
        {
            IPartitionResolver resolver = new MongoDBSessionPartitionResolver();
            string mongoDbConnectionString = resolver.ResolvePartition(sessionId);

            MongoClient client = new MongoClient(mongoDbConnectionString);
            MongoServer srv = client.GetServer();
            MongoDatabase db = srv.GetDatabase(SessionConfiguration.MongoDBName);
            if (!db.CollectionExists(SessionConfiguration.MongoDBCollectionName))
                db.CreateCollection(SessionConfiguration.MongoDBCollectionName);

            MongoCollection<MongoDBSessionEntity> collection = db.GetCollection<MongoDBSessionEntity>(SessionConfiguration.MongoDBCollectionName);

            return collection;
        }

        public static string Serialize(SessionStateItemCollection items)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            if (items != null)
                items.Serialize(writer);

            writer.Close();

            return Convert.ToBase64String(ms.ToArray());
        }


        public static SessionStateStoreData Deserialize(HttpContext context, string serializedItems, int timeout)
        {
            MemoryStream ms = null;
            if(serializedItems!=null&&serializedItems.Trim().Length>0)
                ms = new MemoryStream(Convert.FromBase64String(serializedItems));

            SessionStateItemCollection sessionItems =
              new SessionStateItemCollection();

            if (ms!=null&&ms.Length > 0)
            {
                BinaryReader reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }

            return new SessionStateStoreData(sessionItems,
              SessionStateUtility.GetSessionStaticObjects(context),
              timeout);
        }

        public static SessionStateItemCollection DeserializeCore(string serializedItems)
        {
            SessionStateItemCollection sessionItems = new SessionStateItemCollection();

            MemoryStream ms = null;
            if (serializedItems != null && serializedItems.Trim().Length > 0)
                ms = new MemoryStream(Convert.FromBase64String(serializedItems));

            if (ms != null && ms.Length > 0)
            {
                BinaryReader reader = new BinaryReader(ms);
                sessionItems = SessionStateItemCollection.Deserialize(reader);
            }

            return sessionItems;
        }
    }
}